package example.demo.component;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.web.servlet.context.ServletWebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.Ordered;
import org.springframework.core.PriorityOrdered;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

//registerBeanPostProcessors(beanFactory);加入di的bean-post-processor
@Component
@Slf4j
public class CustomerBeanPostProcessor implements BeanPostProcessor, PriorityOrdered, EnvironmentAware,
        ApplicationListener<ServletWebServerInitializedEvent>, InitializingBean {
    Environment environment;

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
//        log.info("bean--{}-----beanname{}", bean.toString(), beanName);
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
//        log.info("bean--{}-----beanname{}", bean.toString(), beanName);
        return bean;
    }

    @Override
    public int getOrder() {
        return Ordered.HIGHEST_PRECEDENCE;
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }

    //失效
    @PostConstruct
    public void init() {
        log.info("CustomerBeanPostProcessor-----@PostConstruct=init()");
    }

    //OK
    @Override
    public void afterPropertiesSet() throws Exception {
        log.info("CustomerBeanPostProcessor-----InitializingBean");
    }

    @Override
    public void onApplicationEvent(ServletWebServerInitializedEvent event) {
        System.out.println();
    }


}
